package cn.fan.web;

import cn.fan.constants.DataBaseConstants;
import cn.fan.pojo.UserInvest;
import cn.fan.service.UserInvestService;
import cn.fan.utils.MyPage;
import cn.fan.utils.R;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("userinvests")
public class UserInvestController {
    @Autowired
    private UserInvestService userInvestService;

    @GetMapping("test")
    public String test(){
        return "hello,test userinvests!!";
    }


    @PostMapping("add")
    public R add(@RequestBody UserInvest userInvest){
        int i = userInvestService.add(userInvest);
        return i > DataBaseConstants.NO_DATA ? R.ok("添加成功!") : R.error("添加失败!");
    }

    @GetMapping("delete/{id}")
    public R delete(@PathVariable("id") int id){
        int i = userInvestService.delete(id);
        return i > DataBaseConstants.NO_DATA ? R.ok("删除成功!") : R.error("删除失败!");
    }

    @PostMapping("edit")
    public R edit(@RequestBody UserInvest userInvest){
        int i = userInvestService.edit(userInvest);
        return i > DataBaseConstants.NO_DATA ? R.ok("修改成功!") : R.error("修改失败!");
    }

    @PostMapping("list")
    public R list(@RequestBody UserInvest userInvest){
        List<UserInvest> list = userInvestService.list(userInvest);
        Map<String,Object> res = new HashMap<>();
        res.put("list",list);
        return R.ok(res);
    }

    @PostMapping("list/{pageSize}/{currentPage}")
    public R page(@RequestBody UserInvest userInvest,@PathVariable("pageSize") long pageSize,@PathVariable("currentPage") long currentPage){
        IPage<UserInvest> page = userInvestService.page(userInvest,currentPage,pageSize);
        MyPage pagination=new MyPage(page);
        Map<String,Object> res = new HashMap<>();
        res.put("pagination",pagination);
        return R.ok(res);
    }

    @GetMapping("getById/{id}")
    public UserInvest getById(@PathVariable("id") int id){
        return userInvestService.getById(id);
    }

    @GetMapping("overview")
    public R overview(){
        List<Map<String,Object>> overview = userInvestService.overview();
        Map<String,Object> res = new HashMap<>();
        res.put("overview",overview);
        return R.ok(res);
    }

}
